package com.olap.starter.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.olap.starter.core.base.BaseController;
import com.olap.starter.core.response.Result;
import com.olap.starter.pojo.TableConfig;
import com.olap.starter.pojo.TableOperationBut;
import com.olap.starter.pojo.TreeConfig;
import com.olap.starter.service.FormMainServices;
import com.olap.starter.service.TableOperationButServices;
import com.olap.starter.service.TreeConfigServices;
import com.olap.starter.util.DateUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * <p>
 * 树形配置 前端控制器
 * </p>
 *
 * @author WBG
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/online/treeConfig")
public class TreeConfigController extends BaseController<TreeConfig, TreeConfigServices> {

    @PostMapping("/saveJson")
    @Transactional
    public Result<?> saveJson(@RequestBody Map<String, Object> params) {
        TreeConfig treeConfig = this.getService().saveJson(params);
        return success(treeConfig,"保存成功！");
    }

    @GetMapping("/getJson")
    public Result<?> getJson(String uuid) {
        return success(this.getService().getJson(Long.valueOf(uuid)));
    }


    @GetMapping("/download")
    public void download(String uuids, HttpServletResponse response) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        try {
            for (String uuid : uuids.split(",")) {
                Map<String, Object> jsonUUid = this.getService().getJson(Long.valueOf(uuid));
                zip.putNextEntry(new ZipEntry(uuid + ".json"));
                IOUtils.write(JSONObject.toJSONString(jsonUUid), zip, "UTF-8");
                zip.flush();
            }
            zip.closeEntry();
            genCode(response, outputStream.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(zip);
        }
    }

    private void genCode(HttpServletResponse response, byte[] data) throws IOException {
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"online_" + DateUtils.parseDateToStringNow("yyyyMMddHHmmssSSS") + ".zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile[] files) {
        getService().saveJSON(files);
        return success("上传成功！");
    }
}
